#!/usr/bin/python
# -*- coding: utf-8 -*-
import gtk
import gobject
import gettext
from Message import Message
_ = gettext.gettext
class ConfigView(gtk.VBox):
    def __init__(self, function):
        super(ConfigView, self).__init__()
        
        frame = gtk.Frame(_('Config'))
        frame.set_border_width(10)
        frame.set_size_request(100, 75)
        
        hbox = gtk.HBox()
        
        cancelButton = gtk.Button()
        cancelButton.set_label(_('Cancel'))
        cancelButton.set_tooltip_text(_('reload old configuration'))
        cancelButton.connect('clicked', self.loadConfig)
        hbox.pack_end(cancelButton, expand=False, fill=False, padding=0)
        
        saveButton = gtk.Button()
        saveButton.set_label(_('Save'))
        saveButton.set_tooltip_text(_('save new configuration'))
        saveButton.connect('clicked', self.saveConfig)
        hbox.pack_end(saveButton, expand=False, fill=False, padding=0)
        
        innerVBox = gtk.VBox()
        
        self.emailEntry = gtk.Entry()
        self.emailEntry.set_tooltip_text(_('Email used to identify your download list'))
        eHBox = gtk.HBox()
        eHBox.pack_start(gtk.Label(_('Email:')), expand=False, fill=False, padding=0)
        eHBox.pack_start(self.emailEntry, expand=False, fill=False, padding=0)
        innerVBox.pack_start(eHBox, expand=False, fill=False, padding=5)
        
        
        folderDialog = gtk.FileChooserDialog(_('Open folder...'), 
                                            None, 
                                            gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, 
                                            (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                            gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        self.folderButton = gtk.FileChooserButton(folderDialog)
        self.folderButton.set_tooltip_text(_('Choose the location to store downloaded files'))
        dHBox = gtk.HBox()
        dHBox.pack_start(gtk.Label(_('Download To:')), expand=False, fill=False, padding=0)
        dHBox.pack_start(self.folderButton, expand=False, fill=False, padding=0)
        innerVBox.pack_start(dHBox, expand=False, fill=False, padding=5)
        frame.add(innerVBox)
        
        super(ConfigView, self).pack_start(frame, expand=True, fill=True, padding=0)
        super(ConfigView, self).pack_start(hbox, expand=False, fill=False, padding=0)
        super(ConfigView, self).show_all()
        self.function = function
        self.loadConfig(None)        
        
    def saveConfig(self, event):
        filename = '.RDM.cfg'
        file = open(filename, 'w+')
        try:
            email = self.emailEntry.get_text()
            path = self.folderButton.get_filename()
            if email and path:
                file.write(email+'\n')
                file.write(path+'\n')
                gobject.idle_add(self.getConfig)
            else:
                Message.configError()
        finally:
            file.close()
        return True
        
    def loadConfig(self, event):
        filename = '.RDM.cfg'
        try:
            file = open(filename, 'r')
            try:
                email = file.readline().split('\n')[0]
                path = file.readline().split('\n')[0]
                if email and path:
                    self.emailEntry.set_text(email)
                    self.folderButton.set_filename(path)
                    gobject.idle_add(self.getConfig)
                else:
                    Message.configError()
            finally:
                file.close()
        except IOError:
            Message.configError()
        return True

    def getConfig(self):
        email = self.emailEntry.get_text()
        path = self.folderButton.get_filename()
        if email and path:
            self.function([email, path])
